﻿using System;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using UnityEngine;

[Obsolete]
public class NpcMoveBehaviour : MovableEntity
    {
        private GameObject _target;
        public bool isStartingBehavior;

        private void Start()
        {
            currentCollider = GetComponent<Collider2D>();
            InitialMovement();

            GetCurrentTile();
        }

        // Update is called once per frame
        private void Update()
        {
            if (!isMyTurn)
            {
                isStartingBehavior = false;
                return;
            }

            if (isStartingBehavior) return;
            
            isStartingBehavior = true;
            
            BehaviorTree bt = GetComponent<BehaviorTree>();
            
            if (bt)
            {
                bt.EnableBehavior();
            }
        }


        public void CalculateTargetTile()
        {
            if (isMoving)
            {
                return;
            }

            Debug.Log("Target :: 准备计算目标Tile");
        
            GameObject player = GameObject.FindGameObjectWithTag(StaticDataManager.PLAYER_TAG);

            TileHelper.CalculateTileScore(player);

            Vector2 checkBox = new Vector2(move, move);

            Debug.Log("Target :: 寻找最近的移动目标");

            // TODO 寻找这个目标的附近可移动格子
            // TODO 需要优化 最好是移动到掩体后面 除非太远才需要赶路
            this._target = FindValuableTile(this.gameObject, checkBox);

            //this.target = target;

            Debug.Log("Target :: 使用A*计算移动到目标最近的路径");
            CalculatePath();            // 使用A*计算到目标位置的路径

            Debug.Log("Target :: 判断目标是否存在");
            if (actualTargetTile)
            {
                Debug.Log("Target :: 目标已发现");
            }

            if (!actualTargetTile)
            {
                Debug.Log("Target :: 目标未发现");
                actualTargetTile = GetTargetTile(gameObject);
            }

            actualTargetTile.isTarget = true;
        }


        public static GameObject FindValuableTile(GameObject target, Vector2 checkBox)
        {
            // 根据当前的检测BOX来选择分数最高的一个格子
            Collider2D[] collides = Physics2D.OverlapBoxAll(target.transform.position, checkBox, 0, LayerMask.GetMask(StaticDataManager.TILE_LAYER));

            GameObject targetTile = null;
            int currentScore = int.MinValue;
        
            foreach (Collider2D collider in collides)
            {
                if (!collider) continue;
                TileMonoBehaviour tile = collider.gameObject.GetComponent<TileMonoBehaviour>();

                if (tile.isOcupied)  continue;

                int tileScore = tile.GetScore();

                if (tileScore <= currentScore) continue;
                
                currentScore = tileScore;
                targetTile = tile.gameObject;
            }

            return targetTile;
        }

        public bool MoveStep()
        {
            Move();
            return isMoving;
        }

        private void CalculatePath()
        {
            TileMonoBehaviour targetTile = GetTargetTile(_target);
            FindPath(targetTile);
        }

        private IEnumerable<GameObject> FindNearestTarget(string targetTag)
        {
            Debug.Log("Target :: 准备进行计算距离");
            Dictionary<float, GameObject> targetDict = new Dictionary<float, GameObject>();
            List<float> targetDistanceList = new List<float>();

            Collider2D[] targets = Physics2D.OverlapBoxAll(
                transform.position, 
                new Vector2(15, 15), 0f, LayerMask.GetMask(targetTag));

            Debug.Log("Target :: 开始计算距离");

            foreach (Collider2D obj in targets)
            {
                float d = Vector2.Distance(transform.position, obj.gameObject.transform.position);
                targetDistanceList.Add(d);
                targetDict[d] = obj.gameObject;
            }

            Debug.Log("Target :: 开始排序");

            targetDistanceList.Sort();

            Debug.Log("Target :: 排序完成");
            
            while (targetDistanceList.Count > 0)
            {
                Debug.Log("Target :: 开始POP");
                GameObject nearest = targetDict[targetDistanceList.Pop()];

                if (!nearest.CompareTag(StaticDataManager.TILE_TAG))
                {
                    nearest = TileHelper.GetTileByPosition(nearest.transform.position);
                }

                yield return nearest;
            }

            yield return null;
        }
    }